/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BankAccountDetail;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class AccountDetailListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"AccountDetailList.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(1);
		boldStyle.setBorderTop(1);
		boldStyle.setBorderLeft(1);
		boldStyle.setBorderRight(1);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(1);
		norStyle.setBorderTop(1);
		norStyle.setBorderLeft(1);
		norStyle.setBorderRight(1);
		HSSFSheet sheet = workbook.createSheet("list");
		List list = (List) model.get("list");
		String baName = (String) model.get("baName");
		List baList = (List) model.get("baList");
		String rcType = (String) model.get("rcType");
		String listType = (String) model.get("listType");
		if (rcType == null) {
			rcType = "";
		}

		String title;
		String totalStr;
		if (rcType.equals("P")) {
			title = "支出明细表";
			totalStr = "支出小计：" + model.get("pcostTotal").toString() + "元";
		} else if (rcType.equals("R")) {
			title = "收入明细表";
			totalStr = "收入小计：" + model.get("rcostTotal").toString() + "元";
		} else {
			title = "收支明细表";
			totalStr = "收入小计：" + model.get("rcostTotal").toString() + "元 支出小计："
					+ model.get("pcostTotal").toString() + "元";
		}

		title = baName + title + "（" + model.get("startDate").toString()
				+ " 至 " + model.get("endDate").toString() + "）";
		String dateTitle;
		byte colNum;
		if (listType != null && listType.equals("LOG")) {
			dateTitle = "记账日期";
			colNum = 9;
		} else {
			dateTitle = "日期";
			colNum = 8;
		}

		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);

		int i;
		for (i = 1; i < colNum; ++i) {
			r.createCell((short) i);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString(title));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);

		for (i = 1; i < colNum; ++i) {
			r.createCell((short) i);
		}

		c.setCellValue(new HSSFRichTextString(totalStr));
		sheet.addMergedRegion(new Region(1, 0, 1, (short) (colNum - 1)));
		r = sheet.createRow(2);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString(dateTitle));
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("类型"));
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("摘要"));
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("经办人"));
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("收入"));
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("支出"));
		if (listType != null && listType.equals("LOG")) {
			c = r.createCell(7);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("账户余额"));
			c = r.createCell(8);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("账户"));
		} else {
			c = r.createCell(7);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("账户"));
		}

		boolean arg28 = false;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		for (i = 0; i < list.size(); ++i) {
			BankAccountDetail os = (BankAccountDetail) list.get(i);
			BigDecimal rCost;
			BigDecimal pCost;
			if (os.getRcType().equals("R")) {
				rCost = os.getCost();
				pCost = new BigDecimal(0);
			} else {
				rCost = new BigDecimal(0);
				pCost = os.getCost();
			}

			String dateStr;
			if (listType != null && listType.equals("LOG")) {
				dateStr = longSf.format(os.getCostDate());
			} else {
				dateStr = sf.format(os.getBillDate());
			}

			r = sheet.createRow(i + 3);
			c = r.createCell(0);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(dateStr));
			c = r.createCell(1);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(os.getBillCode()));
			c = r.createCell(2);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(os.getCostType()));
			c = r.createCell(3);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(os.getMark()));
			c = r.createCell(4);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(os.getManagerName()));
			c = r.createCell(5);
			c.setCellStyle(norStyle);
			c.setCellValue(rCost.doubleValue());
			c = r.createCell(6);
			c.setCellStyle(norStyle);
			c.setCellValue(pCost.doubleValue());
			if (listType != null && listType.equals("LOG")) {
				c = r.createCell(7);
				c.setCellStyle(norStyle);
				c.setCellValue(os.getBaCost().doubleValue());
				c = r.createCell(8);
				c.setCellStyle(norStyle);
				c.setCellValue(new HSSFRichTextString(EncCommonHelper
						.getSimpleObjectName(baList, os.getBankAccountId())));
			} else {
				c = r.createCell(7);
				c.setCellStyle(norStyle);
				c.setCellValue(new HSSFRichTextString(EncCommonHelper
						.getSimpleObjectName(baList, os.getBankAccountId())));
			}
		}

		ServletOutputStream arg29 = response.getOutputStream();
		workbook.write(arg29);
		arg29.flush();
		arg29.close();
	}
}